package com.woniuxy.woniubook.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubook.entity.Book;
import com.woniuxy.woniubook.service.BookService;
import com.woniuxy.woniubook.service.EsBookService;
import com.woniuxy.woniubook.vo.EsBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wugd
 * @since 2022-11-16 02:13:11
 */
@Controller
@RequestMapping("book")
public class BookController {


    @Autowired
    BookService bookService ;

    @Autowired
    EsBookService esBookService ;

    @RequestMapping("/searchView")
    public String searchView(String searchName,Map map){
        map.put("searchName",searchName);
        return "searchBook" ;
    }

    @RequestMapping("/showBookView")
    public String showBookView(int typeid, Map map){
        map.put("typeid",typeid);
        return "book" ;
    }

    @RequestMapping("/searchBook")
    @ResponseBody
    public List<EsBook> searchBook(String keyWords){
        List<EsBook> bookByEs = esBookService.getBookByEs(keyWords);
        return bookByEs ;
    }

    @RequestMapping("/showBook")
    @ResponseBody
    public Page<Book> bookPage(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            String searchName,Integer tid){

        Page<Book> page = bookService.getBookByPageAndTypeId(now, size, tid, searchName);
        return  page;
    }

    @RequestMapping("/singleBook")
    public String singleBook(int bid,Map map){
        Book book = bookService.getById(bid);
        map.put("book",book);
        return "singleBook";
    }
}

